// TypeScript file
enum TEBPlayerStep {
    BEFORE_GAME = -1,
    START_GAME = 0,
    BANKER = 1,
    BETTING = 2,
    OPEN_CARD = 3,
    FIRST_PLAYER = 4,
    GRAB_BANKER = 5
}
class TEBPlayerView extends V.PlayerView/*how.module.View*/ {
    // private type: number;//位置类型：上下：0   左右：1
    //界面
    private card1: eui.Image;//牌背景
    private card2: eui.Image;//牌背景
    private card1Score: eui.Image;//牌数
    private card2Score: eui.Image;//牌数
    private bankerFlag: eui.Image;//庄家标志
    private playerName: eui.Label;//昵称
    private playerScore: eui.Label;//金币
    private playerHeader: eui.Image;//玩家头像
    //得分
    private winNumberGroup: eui.Group;//加分界面
    private loseNumberGroup: eui.Group;//减分界面
    private reduceScore: eui.BitmapLabel;//减分分数
    private addScore: eui.BitmapLabel;//加分分数
    private playerTimeout: number = -1;
    //点数
    private pointGroup: eui.Group;//点数
    private pointBg: eui.Image;//分数背景
    private pointNuberGroup: eui.Group;
    private pointNumber: eui.Image;//最终点数
    // private pointLight: how.Animation;//动画
    //随机庄家动画
    private rankLight: eui.Image;//随机选中设置
    private randomInterval: number = -1;
    //第一位发牌的人
    private firstFloat: eui.Image;//箭头
    //抢庄
    private grabGroup: eui.Group;//倍数
    private bankerTimes: eui.BitmapLabel;//倍数
    private noGrabImage: eui.Image;//抢庄状态
    //倍投
    private betGroup: eui.Group;//倍投
    private betTimes: eui.BitmapLabel;//倍数
    //计时器
    private openTimeOut1: number = -1;
    private openTimeOut2: number = -1;
    private typeTimeOut: number = -1;
    //步骤
    private step: number = TEBPlayerStep.BEFORE_GAME;
    //牌面信息
    private cardList: Array<number> = [];
    private cardValue: number;
    //庄动画
    private bankerAnimation: how.Animation;
    private cardAnimation: how.Animation;
    private winAnimation: how.Animation;
    //思考动画
    public thinking: eui.Group;
    public thinking0: eui.Image;
    public thinking1: eui.Image;
    public thinking2: eui.Image;

    public constructor() {
        super();
    }
    // public setType(type: number) {
    //     this.type = type;
    // }
    //设置坐下UI（玩家进入坐下的状态）
    // // public setSitUI(data: any): void {
    public show(): boolean {
        super.show();
        // if (data) {
            // this.visible = true;
            // this.resetUI(data);
            this.resetUI();
        // }
        return true;
    }
    // public resetUI(data: any) {
    public resetUI() {
        this.step = TEBPlayerStep.BEFORE_GAME;
        this.betGroup.visible = false;
        this.grabGroup.visible = false;
        this.card1.visible = false;
        this.card2.visible = false;
        this.card1Score.visible = false;
        this.card2Score.visible = false;
        this.winNumberGroup.visible = false;
        this.loseNumberGroup.visible = false;
        this.noGrabImage.visible = false;
        this.bankerFlag.visible = false;
        this.rankLight.visible = false;
        this.bankerAnimation.visible = false;
        this.pointGroup.visible = false;
        this.cardAnimation.visible = false;
        this.winAnimation.visible = false;
        // this.pointLight.visible = false;
        // // var nickName = data.nickName;
        if(!this.getData()) return;
        var nickName = this.getData().nickName;
        // // if (data.isMyself()) {
        if (this.isMe()) {
            var temp: string = nickName, index = temp.indexOf("_");
            if (index > -1) {
                temp = temp.substr(temp.indexOf("_") + 1, temp.length - index);
            }
            nickName = base.Utils.formatNickName(temp, 7)
        }
        this.playerName.text = nickName;
        // this.playerScore.text = base.Utils.formatCurrency(data.UserScoreInfo.lScore);
        // // this.playerScore.text = base.Utils.formatCurrency(data.money);
        this.playerScore.text = base.Utils.formatCurrency(this.getData().score);
        this.playerHeader.source = this.getData().gender == 1 ? "man" + this.getData().faceID + "_png" : "women" + this.getData().faceID + "_png";
    }
    public setPlayerScore(value: number) {
        this.playerScore.text = base.Utils.formatCurrency(value);
    }
    // {"wGameType":2,"lAddScoreCount":100,lplayerScore:1000,lAddScoreTimes:20}
    public setBetScore(data: any) {
        this.step = TEBPlayerStep.BETTING;
        this.firstFloat.visible = false;
        this.card1.visible = false;
        this.card2.visible = false;
        this.card1.source = "";
        this.card2.source = "";
        this.card1Score.visible = false;
        this.card2Score.visible = false;
        this.card1Score.source = "";
        this.card2Score.source = "";
        this.rankLight.visible = false;
        this.winNumberGroup.visible = false;
        this.loseNumberGroup.visible = false;
        this.noGrabImage.visible = false;
        this.grabGroup.visible = false;
        this.playerScore.text = base.Utils.formatCurrency(data.lplayerScore);
        if (data.wGameType == 2) {
            this.betGroup.visible = true;
            this.betTimes.text = data.lAddScoreTimes;
            if (AppData.getInstance().isGameHide) {
                this.betGroup.alpha = 1;
                this.betGroup.scaleX = 1;
                this.betGroup.scaleY = 1;
            } else {
                this.betGroup.alpha = 0.8;
                egret.Tween.get(this.betGroup).to({ scaleX: 1.4, scaleY: 1.4, alpha: 1 }, 150).to({ scaleX: 0.8, scaleY: 0.8, alpha: 0.8 }, 100)
                    .to({ scaleX: 1, scaleY: 1, alpha: 1 }, 100);
            }
        } else {
            this.betGroup.visible = false;
        }
    }
    /**************************游戏***************************** */
    //游戏开始
    public restart() {
        this.pointGroup.visible = false;
        // if (this.pointLight.visible) {
        //     this.pointLight.stop();
        //     this.pointLight.visible = false;
        // }
        this.betGroup.visible = false;
        this.grabGroup.visible = false;
        this.card1.visible = false;
        this.card2.visible = false;
        this.card1.source = "";
        this.card2.source = "";
        this.pointBg.source = "";
        this.card1Score.visible = false;
        this.card2Score.visible = false;
        this.rankLight.visible = false;
        this.winNumberGroup.visible = false;
        this.loseNumberGroup.visible = false;
        this.bankerFlag.visible = false;
        this.noGrabImage.visible = false;
        this.step = TEBPlayerStep.START_GAME;
        this.cardAnimation.visible = false;
        this.winAnimation.visible = false;
    }
    //第一个开牌的人
    public setFirstAnimation() {
        this.winNumberGroup.visible = false;
        this.loseNumberGroup.visible = false;
        this.step = TEBPlayerStep.FIRST_PLAYER;
        this.firstFloat.visible = true;
        var posY0 = this.firstFloat.y, posY1 = posY0 + 10;
        if (AppData.getInstance().isGameHide) {
            this.firstFloat.y = posY0;
        } else {
            egret.Tween.get(this.firstFloat).to({ y: posY1 }, 300).to({ y: posY0 }, 300).to({ y: posY1 }, 300)
                .to({ y: posY0 }, 300).to({ y: posY1 }, 300).to({ y: posY0 }, 300).to({ y: posY1 }, 300);
        }
    }
    //发牌
    public sendCard() {
        this.winNumberGroup.visible = false;
        this.loseNumberGroup.visible = false;
        this.step = TEBPlayerStep.OPEN_CARD;
        egret.Tween.removeTweens(this.firstFloat);
        this.firstFloat.visible = false;
        this.betGroup.visible = false;
        this.grabGroup.visible = false;
        // var type = this.type;
        this.card1.source = "bar_score_bg_0_1_png";
        this.card2.source = "bar_score_bg_0_1_png";
        this.card1.visible = true;
        this.card2.visible = true;
        this.card1Score.visible = false;
        this.card2Score.visible = false;
    }
    //设置牌还没发
    public unsendCard() {
        this.card1.visible = false;
        this.card2.visible = false;
        this.card1Score.visible = false;
        this.card2Score.visible = false;
    }
    //开奖信息
    public setOpenCard(time: number, list: Array<number>, value: number) {
        this.cardList = list;
        this.cardValue = value;
        this.grabGroup.visible = false;
        this.betGroup.visible = false;
        this.pointNumber.source = "";
        this.step = TEBPlayerStep.OPEN_CARD;
        // var type = this.type;
        if (this.openTimeOut1 != -1) {
            egret.clearTimeout(this.openTimeOut1);
            this.openTimeOut1 = -1;
        }
        this.openTimeOut1 = egret.setTimeout(this.setCard, this, time, 0, list[0]);
        if (this.openTimeOut2 != -1) {
            egret.clearTimeout(this.openTimeOut2);
            this.openTimeOut2 = -1;
        }
        this.openTimeOut2 = egret.setTimeout(this.setCard, this, time + 300, 1, list[1]);
        if (this.typeTimeOut != -1) {
            egret.clearTimeout(this.typeTimeOut);
            this.typeTimeOut = -1;
        }
        this.typeTimeOut = egret.setTimeout(this.setCardType, this, time + 550, value);
    }
    public setOpenCardWithOutAnimation(list: Array<number>, value: number) {
        if (this.openTimeOut1 != -1) {
            egret.clearTimeout(this.openTimeOut1);
            this.openTimeOut1 = -1;
        }
        if (this.openTimeOut2 != -1) {
            egret.clearTimeout(this.openTimeOut2);
            this.openTimeOut2 = -1;
        }
        if (this.typeTimeOut != -1) {
            egret.clearTimeout(this.typeTimeOut);
            this.typeTimeOut = -1;
        }
        this.setCard(0, list[0]);
        this.setCard(1, list[1]);
        this.setCardType(value);
    }
    //设置卡牌
    public setCard(index: number, value: number) {
        this.winNumberGroup.visible = false;
        this.loseNumberGroup.visible = false;
        this.grabGroup.visible = false;
        this.betGroup.visible = false;
        // var type = this.type;
        if (index == 0) {
            egret.clearTimeout(this.openTimeOut1);
            this.openTimeOut1 = -1;
            this.card1.source = "bar_score_bg_0_2_png";
            if (AppData.getInstance().isGameHide) {
                this.card1.source = "bar_score_bg_0_0_png";
                this.card1Score.source = "bar_score_0_" + value + "_png";
                this.card1.visible = true;
                this.card1Score.visible = true;
            } else {
                egret.setTimeout(function () {
                    this.card1.source = "bar_score_bg_0_3_png";
                }, this, 25)
                egret.setTimeout(function () {
                    how.SoundManager.playEffect("bar_open_card_mp3");
                    this.card1.source = "bar_score_bg_0_0_png";
                    this.card1Score.source = "bar_score_0_" + value + "_png";
                    this.card1.visible = true;
                    this.card1Score.visible = true;
                }, this, 50);
            }
        } else {
            egret.clearTimeout(this.openTimeOut2);
            this.openTimeOut2 = -1;
            this.card2.source = "bar_score_bg_0_2_png";
            if (AppData.getInstance().isGameHide) {
                this.card2.source = "bar_score_bg_0_0_png";
                this.card2Score.source = "bar_score_0_" + value + "_png";
                this.card2.visible = true;
                this.card2Score.visible = true;
            } else {
                egret.setTimeout(function () {
                    this.card2.source = "bar_score_bg_0_3_png";
                }, this, 25)
                egret.setTimeout(function () {
                    how.SoundManager.playEffect("bar_open_card_mp3");
                    this.card2.source = "bar_score_bg_0_0_png";
                    this.card2Score.source = "bar_score_0_" + value + "_png";
                    this.card2.visible = true;
                    this.card2Score.visible = true;
                }, this, 50);
            }
        }
    }
    //设置卡牌类型
    public setCardType(value: number) {
        this.grabGroup.visible = false;
        this.betGroup.visible = false;
        egret.clearTimeout(this.typeTimeOut);
        this.typeTimeOut = -1;
        if (value == 0) {
            this.pointBg.source = "bar_point_bg_0_png";
        } else if (value > 20 && value < 30) {
            this.pointBg.source = "bar_point_bg_2_png";
        }
        else {
            this.pointBg.source = "bar_point_bg_1_png";
        }
        this.pointNumber.source = "bar_point_" + value + "_png";
        this.pointNuberGroup.alpha = 0.8;
        if (AppData.getInstance().isGameHide) {
            this.pointNuberGroup.scaleX = 1;
            this.pointNuberGroup.scaleY = 1;
            this.pointNuberGroup.alpha = 1;
        } else {
            if (value == 30) {
                this.cardAnimation.play();
            }
            this.pointBg.scaleX = this.pointBg.scaleY = 0.5;
            egret.Tween.get(this.pointBg).to({ scaleX: 1, scaleY: 1 }, 150);
            egret.Tween.get(this.pointNuberGroup).to({ scaleX: 1.4, scaleY: 1.4, alpha: 1 }, 150).to({ scaleX: 0.8, scaleY: 0.8, alpha: 0.8 }, 100)
                .to({ scaleX: 1, scaleY: 1, alpha: 1 }, 100);
        }
        this.pointGroup.visible = true;
    }
    public setCardResult(list: Array<number>, value: number, score: number) {
        this.winNumberGroup.visible = false;
        this.loseNumberGroup.visible = false;
        this.grabGroup.visible = false;
        this.betGroup.visible = false;
        this.setCard(0, list[0]);
        this.setCard(1, list[1]);
        this.setCardType(value);
        this.setScore(score);
    }
    //设置输赢
    public setScore(value: number) {
        if (value < 0) {
            this.winNumberGroup.visible = false;
            this.loseNumberGroup.visible = true;
            this.loseNumberGroup.y = 40;
            this.loseNumberGroup.scaleX = this.loseNumberGroup.scaleY = 1;
            this.loseNumberGroup.alpha = 1;
            this.reduceScore.text = base.Utils.formatCurrency(value) + "";
        } else {
            this.winNumberGroup.visible = true;
            this.loseNumberGroup.visible = false;
            this.winNumberGroup.y = 40;
            this.winNumberGroup.scaleX = this.winNumberGroup.scaleY = 1;
            this.winNumberGroup.alpha = 1;
            this.addScore.text = "+" + base.Utils.formatCurrency(value);
        }
    }
    //设置结果动画
    public setPrizeAnimation(score: number, playerScore: number) {
        var group: eui.Group;
        var win = false;
        if (score < 0) {
            win = false;
            this.winNumberGroup.visible = false;
            this.loseNumberGroup.visible = true;
            this.reduceScore.text = base.Utils.formatCurrency(score) + "";
            group = this.loseNumberGroup;
        } else {
            win = true;
            this.winNumberGroup.visible = true;
            this.loseNumberGroup.visible = false;
            this.addScore.text = "+" + base.Utils.formatCurrency(score);
            group = this.winNumberGroup;
        }
        if (AppData.getInstance().isGameHide) {
            this.setPlayerScore(playerScore);
        } else {
            group.anchorOffsetX = group.width / 2;
            group.anchorOffsetY = group.height / 2;
            if (AppData.getInstance().isGameHide) {
                group.scaleX = group.scaleY = 1;
                group.alpha = 1;
                group.y = 30;
                this.setPlayerScore(playerScore);
            } else {
                if (win) {
                    egret.Tween.get(this).wait(1000).call(function () {
                        this.winAnimation.play();
                    }, this);
                }
                group.scaleX = group.scaleY = 0.2;
                group.y = 180;
                group.alpha = 0;
                egret.Tween.get(group).wait(1000).to({ scaleX: 1, scaleY: 1, alpha: 1 }, 200).to({ y: 30 }, 200);
                if (this.playerTimeout != -1) {
                    egret.clearTimeout(this.playerTimeout);
                    this.playerTimeout = -1;
                }
                this.playerTimeout = egret.setTimeout(function () {
                    this.setPlayerScore(playerScore);
                }, this, 400);
            }
        }
    }
    //设置是否抢庄
    // {"wGameType":1,"wChairID":2,"wCallTimes":5}
    public setGrabBanker(type: number, times: number) {
        this.firstFloat.visible = false;
        this.card1.visible = false;
        this.card2.visible = false;
        this.card1Score.visible = false;
        this.card2Score.visible = false;
        this.winNumberGroup.visible = false;
        this.loseNumberGroup.visible = false;
        this.step = TEBPlayerStep.GRAB_BANKER;
        this.betGroup.visible = false;
        var target: any;
        if (type == 1) {//普通场
            this.noGrabImage.visible = true;
            this.grabGroup.visible = false;
            target = this.noGrabImage;
            if (times == 0) {
                //不抢
                this.noGrabImage.source = "bar_statu_no_grab_png";
            } else {
                this.noGrabImage.source = "bar_statu_grab_png";
            }
        } else {
            if (times == 0) {
                this.noGrabImage.visible = true;
                this.grabGroup.visible = false;
                this.noGrabImage.source = "bar_statu_no_grab_png";
                target = this.noGrabImage;
            } else {
                this.noGrabImage.visible = false;
                this.grabGroup.visible = true;
                this.bankerTimes.text = "" + times;
                target = this.grabGroup;
            }
        }
        this.pointNuberGroup.alpha = 0.8;
        if (!AppData.getInstance().isGameHide) {
            egret.Tween.get(target).to({ scaleX: 1.4, scaleY: 1.4, alpha: 1 }, 150).to({ scaleX: 0.8, scaleY: 0.8, alpha: 0.8 }, 100)
                .to({ scaleX: 1, scaleY: 1, alpha: 1 }, 100);
        }
    }
    //设置庄家
    public setBankerAnimation() {
        this.rankLight.visible = false;
        // this.rankLight.alpha = 1;
        if (!AppData.getInstance().isGameHide) {
            // egret.Tween.get(this.rankLight).to({ alpha: 0 }, 200).to({ alpha: 1 }, 200).to({ alpha: 0 }, 200)
            //     .to({ alpha: 1 }, 200).to({ alpha: 0 }, 200).to({ alpha: 1 }, 200);
            this.bankerAnimation.play();
            how.SoundManager.playEffect("bar_player_selected_mp3");
        }
        egret.setTimeout(()=>{
            this.setBanker(true);
        }, this, 1800);
    }
    public setBanker(value: boolean) {
        this.bankerFlag.visible = value;
        if (!AppData.getInstance().isGameHide) {
            egret.Tween.get(this.bankerFlag).to({ scaleX: 1.4, scaleY: 1.4, alpha: 1 }, 120).to({ scaleX: 0.8, scaleY: 0.8, alpha: 0.8 }, 80)
                .to({ scaleX: 1, scaleY: 1, alpha: 1 }, 80);
        }
        this.noGrabImage.visible = false;
        this.grabGroup.visible = false;
        this.rankLight.visible = false;
        this.bankerAnimation.visible = false;
    }
    public startRandomBanker() {
        this.rankLight.visible = true;
        this.bankerAnimation.visible = false;
        this.rankLight.alpha = 1;
        if (!AppData.getInstance().isGameHide) {
            this.rankLight.alpha = 0;
            egret.Tween.get(this.rankLight).to({ alpha: 1 }, 100).to({ alpha: 0 }, 100);
            how.SoundManager.playEffect("bar_player_select_mp3");
        }
    }
    public stopBankerAnimation() {
        if (this.randomInterval != -1) {
            egret.clearInterval(this.randomInterval);
            this.randomInterval = -1;
        }
        egret.Tween.removeTweens(this.rankLight);
        this.rankLight.visible = false;
    }
    /****************************思考中************************ */
    public showThinking() {
        // console.log("showThinking", this.chairId);
        this.thinking.visible = true;
        egret.Tween.removeTweens(this.thinking0);
        egret.Tween.removeTweens(this.thinking1);
        egret.Tween.removeTweens(this.thinking2);
        this.thinking0.alpha = 0;
        this.thinking1.alpha = 0;
        this.thinking2.alpha = 0;
        egret.Tween.get(this.thinking0, { loop: true }).to({ alpha: 1 }, 400).wait(400).to({ alpha: 0 }, 400).wait(800);
        egret.Tween.get(this.thinking1, { loop: true }).wait(400).to({ alpha: 1 }, 400).wait(400).to({ alpha: 0 }, 400).wait(400);
        egret.Tween.get(this.thinking2, { loop: true }).wait(800).to({ alpha: 1 }, 400).wait(400).to({ alpha: 0 }, 400);
    }
    public closeThinking() {
        // console.log("closeThinking", this.chairId);
        egret.Tween.removeTweens(this.thinking0);
        egret.Tween.removeTweens(this.thinking1);
        egret.Tween.removeTweens(this.thinking2);
        this.thinking0.alpha = 1;
        this.thinking1.alpha = 1;
        this.thinking2.alpha = 1;
        this.thinking.visible = false;
    }
    /********************************************************* */
    public setLeaveUI() {
        this.visible = false;
    }
    public removeAllTweens() {
        this.closeThinking();
        egret.Tween.removeTweens(this.noGrabImage);
        egret.Tween.removeTweens(this.betGroup);
        egret.Tween.removeTweens(this.firstFloat);
        egret.Tween.removeTweens(this.pointNuberGroup);
        egret.Tween.removeTweens(this.loseNumberGroup);
        egret.Tween.removeTweens(this.winNumberGroup);
        egret.Tween.removeTweens(this.grabGroup);
        egret.Tween.removeTweens(this.rankLight);
        this.bankerAnimation.stop();
        this.cardAnimation.stop();
        this.winAnimation.stop();
        this.bankerAnimation.visible = false;
        this.cardAnimation.visible = false;
        this.winAnimation.visible = false;
        if (this.openTimeOut1 != -1) {
            egret.clearTimeout(this.openTimeOut1);
            this.openTimeOut1 = -1;
        }
        if (this.openTimeOut2 != -1) {
            egret.clearTimeout(this.openTimeOut2);
            this.openTimeOut2 = -1;
        }
        if (this.typeTimeOut != -1) {
            egret.clearTimeout(this.typeTimeOut);
            this.typeTimeOut = -1;
        }
    }
    public resume() {
        this.removeAllTweens();
        switch (this.step) {
            case TEBPlayerStep.OPEN_CARD:
                // this.setOpenCardWithOutAnimation(this.cardList, this.cardValue);
                break;
        }
    }
    //销毁
    public onDestroy(): void {
        this.bankerAnimation.stop();
        this.cardAnimation.stop();
        this.winAnimation.stop();
        // // super.onDestroy();
    }
}